package com.studentmanagement.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.studentmanagement.model.User;

public class UserDAO extends BaseDAO<User> {
    
    // 添加用户
    public int addUser(User user) throws SQLException {
        String sql = "INSERT INTO users (user_id, username, password, role, created_at) " +
                     "VALUES (user_seq.NEXTVAL, ?, ?, ?, SYSDATE)";
        
        return update(sql, user.getUsername(), user.getPassword(), user.getRole());
    }
    
    // 更新用户信息
    public int updateUser(User user) throws SQLException {
        String sql = "UPDATE users SET username = ?, password = ?, role = ? " +
                     "WHERE user_id = ?";
        
        return update(sql, user.getUsername(), user.getPassword(), user.getRole(), user.getUserId());
    }
    
    // 删除用户
    public int deleteUser(int userId) throws SQLException {
        String sql = "DELETE FROM users WHERE user_id = ?";
        return update(sql, userId);
    }
    
    // 根据ID查询用户
    public User getUserById(int userId) throws SQLException {
        String sql = "SELECT * FROM users WHERE user_id = ?";
        
        return queryForObject(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs) throws SQLException {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setCreatedAt(rs.getDate("created_at"));
                return user;
            }
        }, userId);
    }
    
    // 根据用户名查询用户
    public User getUserByUsername(String username) throws SQLException {
        String sql = "SELECT * FROM users WHERE username = ?";
        
        return queryForObject(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs) throws SQLException {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setCreatedAt(rs.getDate("created_at"));
                return user;
            }
        }, username);
    }
    
    // 验证用户登录
    public User validateUser(String username, String password) throws SQLException {
        String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        
        return queryForObject(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs) throws SQLException {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setCreatedAt(rs.getDate("created_at"));
                return user;
            }
        }, username, password);
    }
}

